package com.lei.zktwo;

import android.content.Intent;
import android.content.SharedPreferences;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.lei.mvptwozk.mvp.view.BaseActivty;
import com.lei.zktwo.mvp.contract.LoginContract;
import com.lei.zktwo.mvp.model.LoginModel;
import com.lei.zktwo.mvp.presenter.LoginPresenter;

public class LoginActivity extends BaseActivty<LoginPresenter> implements LoginContract.LoginView {
    private EditText etAppkeyLogin;
    private EditText etNameLogin;
    private EditText etPasswdLogin;
    private Button btLogin;
    private TextView btReg;
    private LoginPresenter loginPresenter;

    @Override
    public int bindlayout() {
        return R.layout.activity_login;
    }

    @Override
    public void initview() {
        Intent intent = getIntent();
        final String appkey = intent.getStringExtra("appkey");
        String name = intent.getStringExtra("name");
        String pwd = intent.getStringExtra("pwd");

        etAppkeyLogin = (EditText) findViewById(R.id.et_appkey_login);
        etNameLogin = (EditText) findViewById(R.id.et_name_login);
        etPasswdLogin = (EditText) findViewById(R.id.et_passwd_login);
        btLogin = (Button) findViewById(R.id.bt_login);
        btReg = (TextView) findViewById(R.id.bt_reg);
        etAppkeyLogin.setText(appkey);
        btReg.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(LoginActivity.this, RegisterActivity.class);
                intent.putExtra("appkey",appkey);
                startActivity(intent);
            }
        });
        btLogin.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                loginPresenter.login();
            }
        });
        etNameLogin.setText(name);
        etPasswdLogin.setText(pwd);
    }

    @Override
    public void inject() {
        loginPresenter = new LoginPresenter(new LoginModel(),this);
    }

    @Override
    public String getkey() {
        return etAppkeyLogin.getText().toString().trim();
    }

    @Override
    public String getname() {
        return etNameLogin.getText().toString().trim();
    }

    @Override
    public String getpasswd() {
        return etPasswdLogin.getText().toString().trim();
    }

    @Override
    public void gomain(String json) {
        if (json!=null){
            Toast.makeText(this, ""+json, Toast.LENGTH_SHORT).show();
            startActivity(new Intent(LoginActivity.this, ZhuActivity.class));
            SharedPreferences.Editor log = getSharedPreferences("log", MODE_PRIVATE).edit();
            log.putBoolean("login",true);
            log.commit();
            finish();
        }
    }
}
